package com.ruoyi.web.controller.mdm;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mdm.domain.ChannelInfo;
import com.ruoyi.mdm.service.IChannelInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 渠道信息Controller
 *
 * @author shiseido
 * @date 2025-02-28
 */
@RestController
@RequestMapping("/mdm/channelInfo")
public class ChannelInfoController extends BaseController {
    @Autowired
    private IChannelInfoService channelInfoService;

    /**
     * 查询渠道信息列表
     */
    @ApiOperation("查询渠道信息列表")
    @PreAuthorize("@ss.hasPermi('mdm:channelInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChannelInfo channelInfo) {
        startPage();
        List<ChannelInfo> list = channelInfoService.selectChannelInfoList(channelInfo);
        return getDataTable(list);
    }

    /**
     * 导出渠道信息列表
     */
    @ApiOperation("导出渠道信息列表")
    @PreAuthorize("@ss.hasPermi('mdm:channelInfo:export')")
    @Log(title = "渠道信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChannelInfo channelInfo) {
        List<ChannelInfo> list = channelInfoService.selectChannelInfoList(channelInfo);
        ExcelUtil<ChannelInfo> util = new ExcelUtil<ChannelInfo>(ChannelInfo.class);
        util.exportExcel(response, list, "渠道信息数据");
    }

    /**
     * 获取渠道信息详细信息
     */
    @ApiOperation("获取渠道信息详细信息")
    @PreAuthorize("@ss.hasPermi('mdm:channelInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(channelInfoService.selectChannelInfoById(id));
    }

    /**
     * 新增渠道信息
     */
    @ApiOperation("新增渠道信息")
    @PreAuthorize("@ss.hasPermi('mdm:channelInfo:add')")
    @Log(title = "渠道信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChannelInfo channelInfo) {

        if (!channelInfoService.checkChannelCodeExist(channelInfo)) {
            return error("渠道编码已存在");
        }

        channelInfo.setCreateBy(getUsername());
        channelInfo.setUpdateBy(getUsername());
        return toAjax(channelInfoService.insertChannelInfo(channelInfo));
    }

    /**
     * 修改渠道信息
     */
    @ApiOperation("修改渠道信息")
    @PreAuthorize("@ss.hasPermi('mdm:channelInfo:edit')")
    @Log(title = "渠道信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChannelInfo channelInfo) {

        if (!channelInfoService.checkChannelCodeExist(channelInfo)) {
            return error("渠道编码已存在");
        }

        channelInfo.setUpdateBy(getUsername());
        return toAjax(channelInfoService.updateChannelInfo(channelInfo));
    }

    /**
     * 删除渠道信息
     */
    @ApiOperation("删除渠道信息")
    @PreAuthorize("@ss.hasPermi('mdm:channelInfo:remove')")
    @Log(title = "渠道信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(channelInfoService.deleteChannelInfoByIds(ids));
    }
}
